/**
 * 
 */
package com.shine.icis.blo.imp;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.shine.cotrun.exp.Export;
import com.shine.cotrun.exp.ExportConf;
import com.shine.cotrun.exp.ExportFactory;
import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.blo.IStockCheckService;
import com.shine.icis.dao.IStockCheckDAO;
import com.shine.icis.dao.imp.StockCheckDAO;
import com.shine.icis.dto.ReturnMsg;
import com.shine.icis.exception.ICISException;
import com.shine.icis.hbean.StockCheckProcStatus;
import com.shine.icis.hbean.StockChk;
import com.shine.icis.hbean.StockChkSurv;

/**
 * @author zsy
 *
 */
public class StockCheckService implements IStockCheckService {
	
	private IStockCheckDAO stockCheckDAO;
	private Map<String, String> stockProMap;//股份核对存储过程名称

	public void setStockCheckDAO(IStockCheckDAO stockCheckDAO) {
		this.stockCheckDAO = stockCheckDAO;
	}
	public void setStockProMap(Map<String, String> stockProMap) {
		this.stockProMap = stockProMap;
	}
	public StockCheckProcStatus getProcStatus(int procID, int queryDate){
		return stockCheckDAO.findProcStatus(procID,queryDate);
	}
	public String getCurrentDate(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String date = sdf.format(new Date());
		StringBuffer _date = new StringBuffer(date);
		_date.insert(6, "-").insert(4, "-");
		return _date.toString();
	}
	
	public String getPreviousBusiDate(String currentDate) {
		int date = Integer.parseInt(currentDate.trim().replaceAll("-", ""));
		int ptd = stockCheckDAO.findPreviousBusiDate(date);
		StringBuffer _date = new StringBuffer(Integer.toString(ptd));
		_date.insert(6, "-").insert(4, "-");
		return _date.toString();
	}
	public String getPreviousDate(String currentDate) {
		int date = Integer.parseInt(currentDate.trim().replaceAll("-", ""));
		int ptd = stockCheckDAO.findPreviousDate(date);
		StringBuffer _date = new StringBuffer(Integer.toString(ptd));
		_date.insert(6, "-").insert(4, "-");
		return _date.toString();
	}
	@Override
	public void txCallCheckProc(String stockProc,int busiDate,String exchangeType) {
		if (stockProMap == null) {
			throw new IllegalArgumentException();
		}
		try {
			//int beginDate = stockCheckDAO.findPreviousBusiDate(busiDate);//存储过程参数，上一个交易日
			String proc = stockProMap.get(stockProc);
			ReturnMsg msg = stockCheckDAO.callCheckProc(proc, busiDate,exchangeType);
		} catch(RuntimeException e) {
			throw e;
		}
	}
	@Override
	public void txCallCollectProc(String stockProc,int busiDate,String exchangeType,String srcFlag) {
		if (stockProMap == null) {
			throw new IllegalArgumentException();
		}
		try {
			//int beginDate = stockCheckDAO.findPreviousBusiDate(busiDate);//存储过程参数，上一个交易日
			String proc = stockProMap.get(stockProc);
			ReturnMsg msg = stockCheckDAO.callCollectProc(proc,busiDate,exchangeType,srcFlag);
		} catch(RuntimeException e) {
			throw e;
		}
	}
	
	@Override
	public ListPage<StockChk> getStockChkPage(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		return stockCheckDAO.findListPage(qp);
	}
	@Override
	public ListPage<StockChkSurv> getStockChkSurvPage(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		return stockCheckDAO.findSurvListPage(qp);
	}
	@Override
	public void txCallBackupProc(String stockProc,int busiDate) {
		if (stockProMap == null) {
			throw new IllegalArgumentException();
		}
		try {
			//int beginDate = stockCheckDAO.findPreviousBusiDate(busiDate);//存储过程参数，上一个交易日
			String proc = stockProMap.get(stockProc);
			ReturnMsg msg = stockCheckDAO.callBackupProc(proc, busiDate);
		} catch(RuntimeException e) {
			throw e;
		}
	}
	@Override
	public void deleteStockSurv(long id,int queryDate) {
		StockChkSurv ai = stockCheckDAO.findById(id);
		if (ai == null) {
			return;
		}
		stockCheckDAO.modifyStockSurv(id,queryDate);
	}
	@Override
	public void addStockSurv(StockChkSurv surv){
		this.stockCheckDAO.save(surv);
	}
	@Override
	public void modifySurvNote(StockChkSurv surv,StockChkSurv oldSurv){
		this.stockCheckDAO.modifyStockSurvNote(surv,oldSurv);
	}
	@Override
	public String csExpStockChk(QueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("股份核对结果");
		cfg.addField("营业部",   "branchNo", String.class, 10);
		cfg.addField("交易类别", "exchangeTypeNote", String.class, 80);
		cfg.addField("TA代码",   "taCode", String.class, 4);
		cfg.addField("股东代码", "stockAccount", String.class, 15);
		cfg.addField("证券代码", "stockCode", String.class, 10);
		cfg.addField("资金账号", "fundAccount", String.class, 10);
		cfg.addField("中登份额", "dzAmount", Double.class);
		cfg.addField("柜台份额", "hsAmount", Double.class);
		cfg.addField("差异类型", "diffTypeNote", String.class, 80);
		cfg.addField("比较类型", "compTypeNote", String.class, 80);
		cfg.addField("备注",     "note", String.class, 120);
		cfg.addField("业务日期", "busiDate", String.class, 8);
		//cfg.addField("操作时间", "formatOperTime", String.class, 20);
		Export<StockChk> exp = ExportFactory.createInstance(expType, cfg);
		
		try {
			exp.init();//初始化工作
			
			//添加额处查询条件
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<StockChk> page = this.stockCheckDAO.findListPage(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = this.stockCheckDAO.findListPage(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
	
	@Override
	public String csExpStockChkSurv(QueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("股份核对历史不平数据");
		cfg.addField("营业部",   "branchNo", String.class, 10);
		cfg.addField("交易类别", "exchangeTypeNote", String.class, 8);
		cfg.addField("TA代码",   "taCode", String.class, 4);
		cfg.addField("股东代码", "stockAccount", String.class, 15);
		cfg.addField("证券代码", "stockCode", String.class, 8);
		cfg.addField("资金账号", "fundAccount", String.class, 10);
		cfg.addField("中登份额", "dzAmount", Double.class);
		cfg.addField("柜台份额", "hsAmount", Double.class);
		cfg.addField("差异类型", "diffTypeNote", String.class, 80);
		cfg.addField("备注",     "note", String.class, 120);
		cfg.addField("开始日期", "validDate", String.class, 10);
		cfg.addField("结束日期", "invalidDate", String.class, 10);
		Export<StockChkSurv> exp = ExportFactory.createInstance(expType, cfg);
		
		try {
			exp.init();//初始化工作
			
			//添加额处查询条件
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<StockChkSurv> page = this.stockCheckDAO.findSurvListPage(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = this.stockCheckDAO.findSurvListPage(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
	/*
	 * 取得股份核对单条记录
	 * @see com.shine.icis.blo.IStockCheckService#getStockChk(long)
	 */
	public StockChk getStockChk(long id){
		return stockCheckDAO.findStockChkById(id);
	}
	/*
	 * 修改备注
	 * @see com.shine.icis.blo.IStockCheckService#editNote(long, java.lang.String)
	 */
	public void modifyNote(long id, String note ){
		this.stockCheckDAO.modifyNote(id, note);
	}
}
